package day13.part2;

import day11.demo1.Woman;

/**
 * @Author juzipi
 * @Date 2021/6/28 16:23
 * @Info
 */
public class demo {
    public static void main(String[] args) {
        Person person = new Student("张三");
        System.out.println(person.name);
        //不能调用子类特有的属性或方法
//        person.getAge();
        //可以强转回子类
        Student student = (Student) person;
        student.age = "30";
        student.getAge();
        //强转异常 ClassCastException
//        Teacher teacher = (Teacher) person;
//        teacher.eat();
        //避免强转异常 使用instanceof 判断是否是这个类的实例
        if (person instanceof Teacher){
            Teacher teacher1 = (Teacher) person;
            teacher1.eat();
            System.out.println("强转为teacher");
        }

        if (person instanceof Student){
            Student student1 = (Student) person;
            student1.eat();
            System.out.println("强转为 student");
        }

        if (person instanceof Object){
            System.out.println("能进");
        }

    }

}
